<template>
  <div style="margin: 40px">
    <el-form :inline="true" :model="queryMember" class="demo-form-inline">
      <el-form-item label="用户昵称：">
        <el-input v-model="queryMember.nickname"></el-input>
      </el-form-item>
      <el-form-item label="手机号：">
        <el-input v-model="queryMember.mobile"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onQuery(1)">查询</el-button>
        <el-button @click="queryMember={}">清空</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border fit>
      <el-table-column label="序号" align="center" width="95" v-slot="scope">
        {{ scope.$index + 1 }}
      </el-table-column>
      <el-table-column prop="mobile" label="手机号" align="center" width="110"/>
      <el-table-column prop="email" label="邮箱" align="center" width="120"/>
      <el-table-column prop="nickname" label="昵称" align="center" width="95"/>
      <el-table-column label="年龄" align="center" width="95" v-slot="scope">
        {{ scope.row.age === null ? '未知' : scope.row.age}}
      </el-table-column>
      <el-table-column label="性别" align="center" width="95" v-slot="scope">
        {{ scope.row.sex === 1 ? '女' : scope.row.sex === 2 ? '男': '未知'}}
      </el-table-column>
      <el-table-column label="是否禁用" align="center" width="95" v-slot="scope">
        {{ scope.row.isDisabled ? '已禁用' : '未禁用'}}
      </el-table-column>
      <el-table-column prop="reason" label="禁用理由" align="center"/>
      <el-table-column label="操作" align="center" width="200"  v-slot="scope">
        <el-button v-if="!scope.row.isDisabled" type="danger" @click="onDisabled(scope.row.id,1)">禁用</el-button>
        <el-button v-else type="success" @click="onDisabled(scope.row.id,0)">启用</el-button>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 20px"
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :current-page="pageNum"
      @current-change="onQuery"
      :total="total"/>

    <el-dialog :visible.sync="visibleMember">
      <el-form :model="member" label-width="120px">
        <el-form-item label="禁用理由">
          <el-input v-model="member.reason"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visibleMember = false">取 消</el-button>
        <el-button type="primary" @click="submitMember">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {MemDisabled, MemPage} from "@/api/member";

export default {
  data() {
    return {
      queryMember: {},
      pageNum: 1,
      pageSize: 2,
      total: 0,
      tableData: [],

      visibleMember:false,
      member:{
        reason:''
      },
    }
  },
  created() {
    this.onQuery(1)
  },
  methods: {
    onQuery(pageNum) {
      this.pageNum = pageNum
      MemPage(this.queryMember, this.pageNum, this.pageSize).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    onDisabled(id,isDisabled){
      this.member.id=id;
      this.member.isDisabled=isDisabled;
      this.member.reason=""
      if (isDisabled===1){
        this.visibleMember=true
      }else {
        this.$confirm('确定要启用该用户吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.doDisabled()
        })
      }
    },
    submitMember(){
      this.doDisabled()
    },
    doDisabled(){
      MemDisabled(this.member).then(res=>{
        if (res.code===200){
          this.$message.success("操作成功")
          this.onQuery(1)
        }else {
          this.$message.error("操作失败")
        }
        this.visibleMember=false
      })
    }
  }
}
</script>

<style>

</style>
